﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TerenXNA
{
    public class Water
    {

        public VertexPositionColorNormalTexture[] waterVertices;
        public VertexBuffer waterBuffer;
        public bool loaded = false;

        public struct VertexPositionColorNormalTexture
        {
            public Vector3 Position;
            public Color Color;
            public Vector3 Normal;
            public Vector2 TextureCoordinate;

            public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
            (
                new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
                new VertexElement(sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0),
                new VertexElement(sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
                new VertexElement(sizeof(float) * 6 + 4, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
            );
        }

        public void load()
        {
            waterVertices = new VertexPositionColorNormalTexture[4];

            waterVertices[0].Position = new Vector3(0, 0, 0);
            waterVertices[0].Color = Color.Blue;
            waterVertices[1].Position = new Vector3(0, 0, 101);
            waterVertices[1].Color = Color.Blue;
            waterVertices[2].Position = new Vector3(101, 0, 0);
            waterVertices[2].Color = Color.Blue;
            waterVertices[3].Position = new Vector3(101, 0, 101);
            waterVertices[3].Color = Color.Blue;

            for (int i = 0; i < waterVertices.Length; i++)
            {
                waterVertices[i].Normal = new Vector3(0, -1, 0);
                waterVertices[i].Normal.Normalize();
            }

            waterBuffer = new VertexBuffer(Game1.device, VertexPositionColorNormalTexture.VertexDeclaration, 4, BufferUsage.WriteOnly);
            waterBuffer.SetData(waterVertices);
            loaded = true;
        }

        public void Draw(int lod)
        {
            if (!loaded) return;
            if (lod > 8) return;
            //Game1.effect.CurrentTechnique = Game1.effect.Techniques["Colored"];
            Game1.device.SetVertexBuffer(waterBuffer);
            Game1.device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
        }
    }
}
